package org.study.mybatis.boot.service;

import com.github.pagehelper.*;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.study.mybatis.boot.mapper.StudentMapper;
import org.study.mybatis.boot.model.Student;

import java.util.List;
import java.util.Map;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-05-24
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public int insert(Student student) {
        return studentMapper.insert(student);
    }

    @Override
    public int insertSelective(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Override
    public int updateByPrimaryKeySelective(Student student) {
        return studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public int updateByPrimaryKey(Student student) {
        return studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Student selectByPrimaryKey(Long id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Student> selectList(Student student) {
        return studentMapper.selectList(student);
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    @Override
    public List<Student> selectPageByMap(int pageNum, int pageSize) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("offset", (pageNum - 1) * pageSize);
        param.put("limit", pageSize);
        return studentMapper.selectPageByMap(param);
    }

    @Override
    public List<Student> selectPageByRowBounds(int pageNum, int pageSize) {
        Map<String, Object> param = Maps.newHashMap();
        RowBounds rowBounds = new RowBounds(pageNum, pageSize);
        return studentMapper.selectPageByRowBounds(param, rowBounds);
    }

    @Override
    public PageInfo<Student> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> startPage2(int pageNum, int pageSize) {
        Page<Student> startPage = PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(startPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> studentList = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentList);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> studentList = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (studentList != null) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageJdk7(int pageNum, int pageSize) {
        Page<Student> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(new ISelect() {
                    @Override
                    public void doSelect() {
                        studentMapper.selectAll();
                    }
                });
        PageInfo<Student> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageJdk8(int pageNum, int pageSize) {
        Page<Student> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> studentMapper.selectAll());
        PageInfo<Student> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfo(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectAll());
        return pageInfo;
    }

    @Override
    public long countJdk7() {
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                studentMapper.selectAll();
            }
        });
        return count;
    }

    @Override
    public long countJdk8() {
        long count = PageHelper.count(() -> studentMapper.selectAll());
        return count;
    }
}
